//
//  TypeDetailViewController.m
//  Travel_M_Travel
//
//  Created by dlios on 15-3-25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TypeDetailViewController.h"
#import "NetworkingHandler.h"
#import "ListCell.h"
#import "DetailViewController.h"
#import "SDRefreshStart.h"
#import "RPSlidingMenu.h"
@interface TypeDetailViewController ()<SDRefreshStartDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UISegmentedControl *segmentedControl;
@property (nonatomic, retain)NSString *typeSeg;
@property (nonatomic, retain)UICollectionView *collectionView;

@end

@implementation TypeDetailViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.page = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //    [self.navigationController setNavigationBarHidden:YES];
    
    
    //    self.title = self.mds.tname;
    
    // 背景图片
    UIImageView *background = [[UIImageView alloc] initWithFrame:self.view.bounds];
    background.image = [UIImage imageNamed:@"background.jpeg"];
    background.userInteractionEnabled = YES;
    [self.view addSubview:background];
    [background release];
    
    // 标题
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(130, 17, 170, 50)];
    title.text = self.mds.tname;
    title.textColor = [UIColor whiteColor];
    [background addSubview:title];
    [title release];
    
    // 返回按钮
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(5, 30, 25, 25);
    [back setBackgroundImage:[[UIImage imageNamed:@"back@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [background addSubview:back];
    
    // 分段控制器
    NSArray *segmentedArray = [[NSArray alloc] initWithObjects:@"最火", @"最近更新", @"经典", nil];
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedArray];
    self.segmentedControl.frame = CGRectMake(0, 64, self.view.frame.size.width, 30);
    [self.segmentedControl setTintColor:[UIColor whiteColor]];
    self.segmentedControl.backgroundColor = [UIColor blackColor];
    [_segmentedControl addTarget:self action:@selector(segmentAction:)
                forControlEvents:UIControlEventValueChanged];
    [self.segmentedControl setSelectedSegmentIndex:0];
    [background addSubview:_segmentedControl];
    [_segmentedControl release];
    
    // tableView的父视图
    UIView *viewS = [[UIView alloc] initWithFrame:CGRectMake(0, 94, self.view.frame.size.width, self.view.frame.size.height - 94)];
    viewS.backgroundColor = [UIColor whiteColor];
    [background addSubview:viewS];
    [viewS release];
    
    // 网络请求
    NSString *str = [[@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=hot&device=iPhone&page=1&per_page=20&status=0&tag_name=" stringByAppendingString:self.mds.tname]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        NSLog(@"%@", resultDic);
        self.arr = [NSMutableArray array];
        NSArray *list = [resultDic objectForKey:@"list"];
        for (NSDictionary *dic in list) {
            List *list = [[List alloc] initWithDic:dic];
            [self.arr addObject:list];
            [list release];
        }
        [_collectionView reloadData];
    }];
    
    // collectionView的flowlayout
    RPSlidingMenuLayout *layout = [[RPSlidingMenuLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, viewS.frame.size.height) collectionViewLayout:layout];
    // collectionView创建
    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    [self.collectionView registerClass:[RPSlidingMenuCell class] forCellWithReuseIdentifier:@"reuse"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [viewS addSubview:self.collectionView];
    // 刷新&加载
    [SDRefreshStart SDRefeshTableView:self.collectionView delegate:self];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

#pragma mark - collectionView 协议方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RPSlidingMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    List *l = [self.arr objectAtIndex:indexPath.item];
    [cell.textLabel setText:l.title];
    NSString *str = [NSString stringWithFormat:@"播放数:%@", [l.playsCounts stringValue]];
    [cell.detailTextLabel setText:str];
    [cell.backgroundImageView setImageWithURL:[NSURL URLWithString:l.albumCoverUrl290]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.list = [_arr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:NO];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == self.arr.count - 1) {
        NSLog(@"%ld", self.arr.count);
        [self footRefesh];
    }
}
#pragma mark - 分段控制方法
- (void)segmentAction:(UISegmentedControl *)segment
{
    
    if (0 == segment.selectedSegmentIndex) {
        NSString *str = [[@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=hot&device=iPhone&page=1&per_page=20&status=0&tag_name=" stringByAppendingString:self.mds.tname]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
            NSLog(@"%@", resultDic);
            self.arr = [NSMutableArray array];
            NSArray *list = [resultDic objectForKey:@"list"];
            for (NSDictionary *dic in list) {
                List *list = [[List alloc] initWithDic:dic];
                [self.arr addObject:list];
                [list release];
            }
            [_collectionView reloadData];
        }];
        self.typeSeg = @"hot";
    }else if(1 == segment.selectedSegmentIndex){
        
        
        NSString *str = [[@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=recent&device=iPhone&page=1&per_page=20&status=0&tag_name=" stringByAppendingString:self.mds.tname] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
            self.arr = [NSMutableArray array];
            NSArray *list = [resultDic objectForKey:@"list"];
            for (NSDictionary *dic in list) {
                List *list = [[List alloc] initWithDic:dic];
                [self.arr addObject:list];
                [list release];
            }
            [_collectionView reloadData];
        }];
        
        self.typeSeg = @"recent";
        
    }else if (2 == segment.selectedSegmentIndex){
        
        
        NSString *str = [[@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=classic&device=iPhone&page=1&per_page=20&status=0&tag_name=" stringByAppendingString:self.mds.tname] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
            self.arr = [NSMutableArray array];
            NSArray *list = [resultDic objectForKey:@"list"];
            for (NSDictionary *dic in list) {
                List *list = [[List alloc] initWithDic:dic];
                [self.arr addObject:list];
                [list release];
            }
            [_collectionView reloadData];
        }];
        self.typeSeg = @"classic";
        
    }
    
}
#pragma mark - 下拉刷新
-(void)headRefesh
{
    
    self.page = 1;
    [self.arr removeAllObjects];
    
    NSString *str = [[NSString stringWithFormat: @"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=%@&device=iPhone&page=1&per_page=20&status=0&tag_name=%@" ,self.typeSeg, self.mds.tname ] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        NSLog(@"%@", resultDic);
        //        self.arr = [NSMutableArray array];
        NSArray *list = [resultDic objectForKey:@"list"];
        for (NSDictionary *dic in list) {
            List *list = [[List alloc] initWithDic:dic];
            [self.arr addObject:list];
            [list release];
        }
        [self.collectionView reloadData];
    }];
    
}
#pragma mark - 上拉加载
- (void)footRefesh
{
    self.page++;
    NSString *pageCount = [NSString stringWithFormat:@"%ld", self.page];
    NSString *str = [[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=music&condition=%@&device=iPhone&page=%@&per_page=20&status=0&tag_name=%@", self.typeSeg, pageCount, self.mds.tname] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        NSLog(@"%@", resultDic);
        NSArray *list = [resultDic objectForKey:@"list"];
        for (NSDictionary *dic in list) {
            List *list = [[List alloc] initWithDic:dic];
            [self.arr addObject:list];
            [list release];
        }
        [self.collectionView reloadData];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
